package org.linlinjava.litemall.admin.job;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.linlinjava.litemall.admin.task.ScheduledCronTaskRegistrar;
import org.linlinjava.litemall.db.dao.*;
import org.linlinjava.litemall.db.domain.*;
import org.linlinjava.litemall.db.service.*;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.List;

@Component
@RequiredArgsConstructor
public class AdminUserOutJob implements Job {


    private final LitemallUserService litemallUserService;

    private final LitemallCartService litemallCartService;

    private final LitemallCollectMapper litemallCollectMapper;

    private final LitemallCommentMapper litemallCommentMapper;

    private final LitemallCouponUserService litemallCouponUserService;

    private final LitemallFeedbackMapper litemallFeedbackMapper;

    private final LitemallFootprintMapper litemallFootprintMapper;

    private final LitemallGiftCardOrderService litemallGiftCardOrderService;

    private final LitemallGiftCardShareMapper litemallGiftCardShareMapper;

    private final LitemallGiftCardUserService litemallGiftCardUserService;

    private final LitemallMessageReceiverService litemallMessageReceiverService;

    //消息订阅表

    private final LitemallNoticeMapper litemallNoticeMapper;

    private final LitemallOrderService litemallOrderService;

    private final LitemallRechargeConsumptionService litemallRechargeConsumptionService;

    private final LitemallRefundService litemallRefundService;

    private final LitemallSearchHistoryMapper litemallSearchHistoryMapper;

    private final LitemallSubscribeUserMapper litemallSubscribeUserMapper;

    private final LitemallUserRechargeOrderMapper litemallUserRechargeOrderMapper;

    private final LumiereCloverCardService lumiereCloverCardService;

    private final LumiereLevelMemberPointService lumiereLevelMemberPointService;

    private final LumierePayMemberUserService lumierePayMemberUserService;

    private final LumierePayMemberUserLogService lumierePayMemberUserLogService;

    private final LumiereSubscribeOrderService lumiereSubscribeOrderService;

    private final LumiereSubscribeUserService lumiereSubscribeUserService;

    private final LumiereSubscribeUserLogService lumiereSubscribeUserLogService;

    private final LitemallAddressService litemallAddressService;

    private final ScheduledCronTaskRegistrar scheduledCronTaskRegistrar;


    /**XX期限后自动删除用户账号信息，切逻辑删除账号一切关联数据*/
    //@Scheduled(cron = "0 0 1 * * ?")
    public void AdminUserOutJob(){
        //找到所有用户自己注销状态的账号
        List<LitemallUser> litemallUserList = litemallUserService.list(Wrappers.lambdaQuery(LitemallUser.class)
                .eq(LitemallUser::getStatus, 1)
                .apply("deleted_by = id")
        );
        //如果今天是注销的第15天则，删除账号，逻辑删除关联数据
        litemallUserList.stream().forEach(litemallUser -> {
            LocalDateTime now = LocalDateTime.now();
            long day = Duration.between(litemallUser.getUpdateTime(), now).toDays();
            if(litemallUser.getUpdateTime().isBefore(now)&&day>=30){
                eraseData(litemallUser);
            }
        });
    }

    @Async
    public void eraseData(LitemallUser litemallUser){
        litemallUser.setUsername("--- ---");
        litemallUser.setPassword("--- --");
        litemallUser.setGender((byte) 0);
        litemallUser.setBirthday(null);
        litemallUser.setUserLevel((byte) 66);
        litemallUser.setNickname("--- --");
        litemallUser.setEmail("--- --");
        litemallUser.setMobile("--- --");
        litemallUser.setAvatar("--- --");
        litemallUser.setWeixinOpenid("--- --");
        litemallUser.setSessionKey("--- --");
        litemallUser.setStatus((byte) 2);
        litemallUser.setDeleted(true);
        litemallUserService.updateById2(litemallUser);
        //litemallCartService.delete(litemallUser.getId());
        litemallCollectMapper.delete(Wrappers.lambdaQuery(LitemallCollect.class).eq(LitemallCollect::getUserId,litemallUser.getId()));
        litemallCommentMapper.delete(Wrappers.lambdaQuery(LitemallComment.class).eq(LitemallComment::getUserId,litemallUser.getId()));
        litemallCouponUserService.update(Wrappers.lambdaUpdate(LitemallCouponUser.class).set(LitemallCouponUser::getDeleted,1).eq(LitemallCouponUser::getUserId,litemallUser.getId()));
        litemallFeedbackMapper.delete(Wrappers.lambdaQuery(LitemallFeedback.class).eq(LitemallFeedback::getUserId,litemallUser.getId()));
        litemallFootprintMapper.update(null,Wrappers.lambdaUpdate(LitemallFootprint.class).eq(LitemallFootprint::getUserId,litemallUser.getId()));
        litemallGiftCardOrderService.update(Wrappers.lambdaUpdate(LitemallGiftCardOrder.class).eq(LitemallGiftCardOrder::getUserId,litemallUser.getId()));
        litemallGiftCardShareMapper.update(null,Wrappers.lambdaUpdate(LitemallGiftCardShare.class).eq(LitemallGiftCardShare::getUserId,litemallUser.getId()));
        litemallGiftCardUserService.update(Wrappers.lambdaUpdate(LitemallGiftCardUser.class).eq(LitemallGiftCardUser::getUserId,litemallUser.getId()));
        litemallMessageReceiverService.update(Wrappers.lambdaUpdate(LitemallMessageReceiver.class).eq(LitemallMessageReceiver::getUserId,litemallUser.getId()));
        litemallNoticeMapper.update(null,Wrappers.lambdaUpdate(LitemallNotice.class).eq(LitemallNotice::getUserId,litemallUser.getId()));
        litemallOrderService.update(Wrappers.lambdaUpdate(LitemallOrder.class).eq(LitemallOrder::getUserId,litemallUser.getId()));
        litemallRechargeConsumptionService.update(Wrappers.lambdaUpdate(LitemallRechargeConsumption.class).eq(LitemallRechargeConsumption::getUserId,litemallUser.getId()));
        litemallRefundService.update(Wrappers.lambdaUpdate(LitemallRefund.class).eq(LitemallRefund::getUserId,litemallUser.getId()));
        litemallSearchHistoryMapper.update(null,Wrappers.lambdaUpdate(LitemallSearchHistory.class).eq(LitemallSearchHistory::getUserId,litemallUser.getId()));
        litemallSubscribeUserMapper.update(null,Wrappers.lambdaUpdate(LitemallSubscribeUser.class).eq(LitemallSubscribeUser::getUserId,litemallUser.getId()));
        litemallUserRechargeOrderMapper.update(null,Wrappers.lambdaUpdate(LitemallUserRechargeOrder.class).eq(LitemallUserRechargeOrder::getUserId,litemallUser.getId()));
        lumiereCloverCardService.update(Wrappers.lambdaUpdate(LumiereCloverCard.class).eq(LumiereCloverCard::getUserId,litemallUser.getId()));
        lumiereLevelMemberPointService.update(Wrappers.lambdaUpdate(LumiereLevelMemberPoint.class).eq(LumiereLevelMemberPoint::getUserId,litemallUser.getId()));
        lumierePayMemberUserService.update(Wrappers.lambdaUpdate(LumierePayMemberUser.class).eq(LumierePayMemberUser::getUserId,litemallUser.getId()));
        lumierePayMemberUserLogService.update(Wrappers.lambdaUpdate(LumierePayMemberUserLog.class).eq(LumierePayMemberUserLog::getUserId,litemallUser.getId()));
        lumiereSubscribeOrderService.update(Wrappers.lambdaUpdate(LumiereSubscribeOrder.class).eq(LumiereSubscribeOrder::getUserId,litemallUser.getId()));
        lumiereSubscribeUserService.update(Wrappers.lambdaUpdate(LumiereSubscribeUser.class).eq(LumiereSubscribeUser::getUserId,litemallUser.getId()));
        lumiereSubscribeUserLogService.update(Wrappers.lambdaUpdate(LumiereSubscribeUserLog.class).set(LumiereSubscribeUserLog::getDeleted,1).eq(LumiereSubscribeUserLog::getUserId,litemallUser.getId()));
        litemallAddressService.update(Wrappers.lambdaUpdate(LitemallAddress.class).set(LitemallAddress::getDeleted,1).eq(LitemallAddress::getUserId,litemallUser.getId()));
    }

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        this.AdminUserOutJob();
        scheduledCronTaskRegistrar.scheduleJobLog(jobExecutionContext);
    }
}
